package com.rzico.sbl.ui.statistic

import android.widget.ImageView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCompleteBinding
import com.rzico.sbl.model.OrderFooter
import com.rzico.sbl.model.OrderGoods
import com.rzico.sbl.model.OrderHeader
import com.rzico.sbl.other.*
import com.rzico.sbl.ui.order.OrderDetailActivity
import com.rzico.sbl.viewmodel.StatisticOrderViewModel
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.extend.*
import com.xinnuo.common_ui.base.*
import com.xinnuo.common_ui.glideExt.loadImage
import com.xinnuo.common_ui.utils.setEmptyImage
import com.xinnuo.common_ui.utils.showTimeRangeDialog
import com.xinnuo.slimadapter.SlimAdapter

class IncompleteActivity : BaseActivity(R.layout.activity_complete) {

    private val mBinding by lazy { ActivityCompleteBinding.bind(getRootView()) }

    private var mOrderType = ""
    private var mOrderPay = ""
    private var mBeginDate = getTodayDate()
    private var mEndDate = getTodayDate()

    override fun getViewModel() = get<StatisticOrderViewModel>()

    override fun initData() {
        initTitle(title = "未完成订单")

        initLayout()

        mBinding.listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyImg.setEmptyImage(
                    resId = R.mipmap.icon_order_no,
                    dpSize = 100f
                )
                it.emptyHint.text = "暂无相关订单信息！"
            }

            recycleList.also { layout ->
                layout.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                mAdapter = SlimAdapter.creator()
                    .register<OrderHeader>(R.layout.item_statistic_order_title) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_order_time, bean.hint + bean.time)
                            text(R.id.item_order_status, bean.status_hint)
                            clicked {
                                startActivityEx<OrderDetailActivity>("orderSn" to bean.orderSN)
                            }
                        }
                    }
                    .register<OrderGoods>(R.layout.item_statistic_order_content) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_order_title, "${bean.consignee}  ${bean.phone}")
                            text(R.id.item_order_name, bean.name)
                            text(R.id.item_order_price, "单价：${bean.price.formatDecimal()} 元")
                            text(R.id.item_order_num, "x${bean.quantity}")

                            with<ImageView>(R.id.item_order_img) {
                                it.loadImage(bean.thumbnail)
                            }

                            with<ImageView>(R.id.item_order_img_type) {
                                when (bean.methodDescr) {
                                    "offline" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order03)
                                    }

                                    "eleme", "elemengc" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order04)
                                    }

                                    "meituan" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order05)
                                    }

                                    "nongfu" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order06)
                                    }

                                    "jd" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order10)
                                    }

                                    "taobao" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order11)
                                    }

                                    "pinduoduo" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order12)
                                    }

                                    "dummy" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order22)
                                    }

                                    "ali" -> {
                                        it.visible()
                                        it.setImageResource(R.mipmap.icon_order26)
                                    }

                                    else -> it.gone()
                                }
                            }

                            clicked {
                                startActivityEx<OrderDetailActivity>("orderSn" to bean.orderSN)
                            }
                        }
                    }
                    .register<OrderFooter>(R.layout.item_statistic_order_foot) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_order_count, "共 ${bean.total_num} 件商品")
                            text(R.id.item_order_total, bean.amount.formatDecimal())

                            clicked {
                                startActivityEx<OrderDetailActivity>("orderSn" to bean.orderSN)
                            }
                        }
                    }
                    .attachTo(layout)
            }
        }
    }

    override fun initListener() {
        mBinding.apply {
            completeTypeLl.oneClick {
                getViewModel().showDropFilter(
                    getIContext(),
                    completeTop,
                    getViewModel().mOrderTypeList,
                    completeType,
                    completeTypeArrow
                ) {
                    if (it.id.isEmpty()) {
                        completeType.setTextColor(getColorEx(R.color.black))
                        completeTypeArrow.setImageResource(R.mipmap.icon_member01)
                    } else {
                        completeType.setTextColor(getColorEx(R.color.colorAccent))
                        completeTypeArrow.setImageResource(R.mipmap.icon_member02)
                    }

                    mOrderType = it.id
                    updateList()
                }
            }

            completeWayLl.oneClick {
                getViewModel().showDropFilter(
                    getIContext(),
                    completeTop,
                    getViewModel().mOrderPayList,
                    completeWay,
                    completeWayArrow
                ) {
                    if (it.id.isEmpty()) {
                        completeWay.setTextColor(getColorEx(R.color.black))
                        completeWayArrow.setImageResource(R.mipmap.icon_member01)
                    } else {
                        completeWay.setTextColor(getColorEx(R.color.colorAccent))
                        completeWayArrow.setImageResource(R.mipmap.icon_member02)
                    }

                    mOrderPay = it.id
                    updateList()
                }
            }

            completeTimeLl.oneClick {
                getViewModel().showDropTimeFilter(
                    getIContext(),
                    completeTime,
                    completeTimeArrow,
                    completeTop,
                    getViewModel().mTimeList
                ) { bean ->

                    if (bean.id != "7") {
                        completeTime.setTextColor(getColorEx(R.color.colorAccent))
                        completeTimeArrow.setImageResource(R.mipmap.icon_member02)
                        getViewModel().mTimeList.last().name = "自定义时间"
                    }

                    when (bean.id) {
                        "1" -> {
                            mBeginDate = getTodayDate()
                            mEndDate = getTodayDate()

                            updateList()
                        }
                        "2" -> {
                            mBeginDate = getBeforeDate()
                            mEndDate = getBeforeDate()

                            updateList()
                        }
                        "3" -> {
                            mBeginDate = getMondayDate()
                            mEndDate = getSundayDate()

                            updateList()
                        }
                        "4" -> {
                            mBeginDate = getFirstDate()
                            mEndDate = getEndDate()

                            updateList()
                        }
                        "5" -> {
                            mBeginDate = getBeforeFirstDate()
                            mEndDate = getBeforeEndDate()

                            updateList()
                        }
                        "6" -> {
                            mBeginDate = getYearFirstDate()
                            mEndDate = getYearEndDate()

                            updateList()
                        }
                        "7" -> {
                            showTimeRangeDialog(
                                isCurrentDate = true,
                                isAfterLimited = true,
                                onResult = { start, end ->
                                    getViewModel()
                                        .mTimeList
                                        .forEach { it.isChecked = false }

                                    getViewModel()
                                        .mTimeList
                                        .last()
                                        .isChecked = true

                                    completeTime.text = "自定义"
                                    completeTime.setTextColor(getColorEx(R.color.colorAccent))
                                    completeTimeArrow.setImageResource(R.mipmap.icon_member02)

                                    mBeginDate = start
                                    mEndDate = end
                                    bean.name = "$mBeginDate  ~  $mEndDate"

                                    updateList()
                                }
                            )
                        }
                    }
                }
            }
        }
    }

    /**
     * 未完成订单列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .orderList(
                page = index,
                status = "uncompleted",
                type = mOrderType,
                pay = mOrderPay,
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = { total, data ->
                    mBinding.completeTotal.text = total.toString()

                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(data)
                        data.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}